package com.xiaolin.config;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.xiaolin.anno.LoginToken;
import com.xiaolin.pojo.User;
import com.xiaolin.service.UserService;
import com.xiaolin.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * JWT权限验证
 *
 * @author XiaoLin
 * @date 2019/2/22 20:51
 */
public class AuthenticationInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {

        // 从 http 请求头中取出 token
        String token = httpServletRequest.getHeader("token");
        // 如果不是映射到方法直接通过
        if (!(object instanceof HandlerMethod)) {
            return true;
        }

        HandlerMethod handlerMethod = (HandlerMethod) object;
        Method method = handlerMethod.getMethod();
        //检查是否有LoginToken注释，有则跳过认证
        if (method.isAnnotationPresent(LoginToken.class)) {
            LoginToken loginToken = method.getAnnotation(LoginToken.class);
            if (loginToken.required()) {
                return true;
            }
        }

        //检查有没有需要用户权限的注解
//        if (method.isAnnotationPresent(CheckToken.class)) {
//            CheckToken checkToken = method.getAnnotation(CheckToken.class);
//            if (checkToken.required()) {

        //其他方法都需要认证

        // 执行认证
        if (token == null) {
            throw new RuntimeException("无token，请重新登录");
        }
        // 获取 token 中的 user id
        String userId;
        try {
            userId = JWT.decode(token).getClaim("id").asString();

        } catch (JWTDecodeException j) {
            throw new RuntimeException("访问异常！");
        }
        //通过userId查询user
        User user = userService.findUserById(userId);
        //保存到请求作用域中备用

        if (user == null) {
            throw new RuntimeException("用户不存在，请重新登录");
        }
        Boolean verify = JwtUtil.isVerify(token, user);
        if (!verify) {
            throw new RuntimeException("非法访问！");
        }
        httpServletRequest.setAttribute("userId", userId);
        httpServletRequest.setAttribute("user", user);
        //清除密码信息
        user.setPassword("");
        return true;
//            }
//        }
//        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }


}